import 'dart:io';

import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

class PermissionUtils {
  static Future isPermission(BuildContext context) async {
    bool permission = true;

    Map<PermissionGroup, PermissionStatus> permissions =
        await PermissionHandler().requestPermissions([
      PermissionGroup.location,
      PermissionGroup.storage,
      PermissionGroup.camera
    ]);

    PermissionStatus permissionStatusLocation =
        permissions[PermissionGroup.location];
    PermissionStatus permissionStatusStorage;
    if (Platform.isIOS) {
      permissionStatusStorage = permissions[PermissionGroup.photos];
    } else {
      permissionStatusStorage = permissions[PermissionGroup.storage];
    }

    PermissionStatus permissionStatusCamera =
        permissions[PermissionGroup.camera];

    if (permissionStatusLocation != PermissionStatus.granted ||
        permissionStatusStorage != PermissionStatus.granted ||
        permissionStatusCamera != PermissionStatus.granted) {
      bool isShownLocation = await PermissionHandler()
          .shouldShowRequestPermissionRationale(PermissionGroup.location);
      bool isShownStorage = await PermissionHandler()
          .shouldShowRequestPermissionRationale(PermissionGroup.storage);
      bool isShownCamera = await PermissionHandler()
          .shouldShowRequestPermissionRationale(PermissionGroup.camera);
      if (!isShownLocation || !isShownStorage || !isShownCamera) {
        permission = false;
         BotToast.showText(text: '请同意相关权限，否则功能无法使用');
      }
    }
    return permission;
  }

  static Future isCameraPermission(BuildContext context) async {
    bool permission = true;

    Map<PermissionGroup, PermissionStatus> permissions =
        await PermissionHandler().requestPermissions(
            [PermissionGroup.storage, PermissionGroup.camera]);
    PermissionStatus permissionStatusStorage;
    if (Platform.isIOS) {
      permissionStatusStorage = permissions[PermissionGroup.photos];
    } else {
      permissionStatusStorage = permissions[PermissionGroup.storage];
    }

    PermissionStatus permissionStatusCamera =
        permissions[PermissionGroup.camera];
    if (permissionStatusStorage != PermissionStatus.granted ||
        permissionStatusCamera != PermissionStatus.granted) {
      bool isShownStorage = await PermissionHandler()
          .shouldShowRequestPermissionRationale(PermissionGroup.storage);
      bool isShownCamera = await PermissionHandler()
          .shouldShowRequestPermissionRationale(PermissionGroup.camera);
      if (!isShownStorage || !isShownCamera) {
        permission = false;
         BotToast.showText(text: '请同意相关权限，否则功能无法使用');
      }
    }
    return permission;
  }

  static Future isStoragePermission(BuildContext context) async {
    bool permission = true;

    Map<PermissionGroup, PermissionStatus> permissions =
    await PermissionHandler().requestPermissions(
        [PermissionGroup.storage, ]);
    PermissionStatus permissionStatusStorage;

      permissionStatusStorage = permissions[PermissionGroup.storage];

    if (permissionStatusStorage != PermissionStatus.granted ) {
      bool isShownStorage = await PermissionHandler()
          .shouldShowRequestPermissionRationale(PermissionGroup.storage);

      if (!isShownStorage) {
        permission = false;
        BotToast.showText(text: '请同意相关权限，否则功能无法使用');
      }
    }
    return permission;
  }


  static Future isLocationPermission(BuildContext context) async {
    bool permission = true;

    Map<PermissionGroup, PermissionStatus> permissions =
        await PermissionHandler().requestPermissions([
      PermissionGroup.location,
    ]);
    PermissionStatus permissionStatusLocation =
        permissions[PermissionGroup.location];

    if (permissionStatusLocation != PermissionStatus.granted) {
      bool isShownLocation = await PermissionHandler()
          .shouldShowRequestPermissionRationale(PermissionGroup.location);

      if (!isShownLocation) {
        permission = false;
         BotToast.showText(text: '请同意相关权限，否则功能无法使用');
      }
    }
    return permission;
  }
}
